package com.hengyi.pda.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.common.utils.EmptyUtils;
import com.common.widget.recyclerview.refresh.adapter.CommonAdapter;
import com.common.widget.recyclerview.refresh.adapter.ViewHolder;
import com.hengyi.pda.R;
import com.hengyi.pda.bean.SubmitBean;

import java.util.HashMap;
import java.util.List;

public class SpareScanAdapter extends CommonAdapter<SubmitBean.APPLYLISTSBean> {
    public HashMap<Integer, String> contents = new HashMap<>();
    public SpareScanAdapter(Context context, List<SubmitBean.APPLYLISTSBean> data) {
        super(context, data);
    }

    @Override
    protected void convert(ViewHolder holder, SubmitBean.APPLYLISTSBean item, int position) {
        holder.setText(R.id.tv_qrcode,item.ID) ;
        EditText etNum = holder.getView(R.id.et_num);
        etNum.addTextChangedListener(new MyTextChangedListener(holder,contents ));
    }

    @Override
    protected int getItemViewLayoutId(int position, SubmitBean.APPLYLISTSBean item) {
        return R.layout.item_spare;
    }

    public class MyTextChangedListener implements TextWatcher {

        public ViewHolder holder;
        public HashMap<Integer, String> contents;


        public MyTextChangedListener(ViewHolder holder,HashMap<Integer, String> contents ){
            this.holder = holder;
            this.contents = contents;

        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if(holder != null && contents != null && EmptyUtils.isNotEmpty(editable)){
                int adapterPosition = holder.getAdapterPosition();
                contents.put(adapterPosition,editable.toString());
                mData.get(adapterPosition-1).APPLY_NUM   = Double.valueOf(editable.toString()) ;
            }
        }
    }
}
